<template>
  <div id="detail" v-title data-title="详情">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info ref="params" :param-info="paramInfo"/>
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommend" :goods="recommends"/>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addToCart="addToCart"/>
    <toast :message="message" :show="show"/>
  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/DetailBaseInfo";
  import DetailShopInfo from "./childComps/DetailShopInfo";
  import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import DetailBottomBar from "./childComps/DetailBottomBar";
  import Scroll from "@/components/common/scroll/Scroll";
  import Toast from "@/components/common/toast/Toast";
  import {
    getDetail,
    Goods,
    Shop,
    GoodsParam,
    getRecommend
  } from "@/network/detail";
  import GoodsList from "@/components/content/goods/GoodsList";
  import {itemListenerMixin, backTopMixin} from "@/common/mixin";
  import {debounce} from "@/common/utils";
  import {mapActions} from 'vuex'

  export default {
    name: "Detail",
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommends: [],
        themeTopYs: [],
        getThemeTopY: null,
        currentIndex: 0,
        message: '',
        show: false
      };
    },
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,
      Scroll,
      Toast,
      GoodsList
    },
    mixins: [itemListenerMixin, backTopMixin],
    created() {
      this.iid = this.$route.params.iid;

      getDetail(this.iid).then(res => {
        // console.log(res);
        // 获取顶部轮播图数据
        const data = res.result;
        this.topImages = data.itemInfo.topImages;

        // 获取商品信息
        this.goods = new Goods(
            data.itemInfo,
            data.columns,
            data.shopInfo.services
        );
        // console.log(this.goods);
        // 获取商店信息
        this.shop = new Shop(data.shopInfo);

        // 保存商品的详情数据
        this.detailInfo = data.detailInfo;

        // 获取参数数据
        this.paramInfo = new GoodsParam(
            data.itemParams.info,
            data.itemParams.rule
        );

        // 获取用户评论数据
        if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
      });
      getRecommend().then(res => {
        this.recommends = res.data.list;
      });
      this.getThemeTopY = debounce(() => {
        this.themeTopYs = [];
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop - 50);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 50);
        this.themeTopYs.push(Number.MAX_VALUE);
      }, 100);
    },
    methods: {
      ...mapActions(['addCart']),
      imageLoad() {
        this.refresh();
        this.getThemeTopY();
      },
      titleClick(index) {
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
      },
      contentScroll(position) {
        let positionY = -position.y;
        let length = this.themeTopYs.length;
        for (let i = 0; i < length - 1; i++) {
          if ((this.currentIndex !== i) && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
            this.currentIndex = i;
            this.$refs.nav.currentIndex = this.currentIndex;
          }
        }
        this.backTopShow(position)
      },
      addToCart() {
        // 获取购物车需要展示的信息
        const product = {}
        product.image = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.nowPrice
        product.iid = this.iid

        // 将商品加入到购物车里
        // 1.actions 普通调用
        // this.$store.dispatch('addCart',product).then(res => {
        //   console.log(res);
        // })
        // 2. mapActions 映射到局部方法
        this.addCart(product).then(res => {
          // console.log(res);
          // this.show = true
          // this.message = res
          //
          // setTimeout(() => {
          //   this.show = false
          //   this.message = ''
          // },1500)
          this.$toast.show(res)
        })
      }
    },
    mounted() {
    },
    destroyed() {
      this.$bus.$off("itemImgLoad", this.itemImgListener);
    }
  };
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 40;
    background-color: #ffffff;
    height: 100vh;
  }

  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #ffffff;
  }

  .content {
    height: calc(100% - 44px - 49px);
  }
</style>
